package com.gimi.cloud.jimi.manage.consumer.service.app.impl;

import com.gimi.cloud.jimi.common.enums.ResponseCodeEnum;
import com.gimi.cloud.jimi.common.exception.MsgException;
import com.gimi.cloud.jimi.common.utils.JsonUtil;
import com.gimi.cloud.jimi.common.utils.StringUtil;
import com.gimi.cloud.jimi.common.utils.Validates;
import com.gimi.cloud.jimi.common.utils.reqhead.Response;
import com.gimi.cloud.jimi.common.utils.reqhead.Util;
import com.gimi.cloud.jimi.dto.app.MajorAdDTO;
import com.gimi.cloud.jimi.manage.consumer.feign.AdAndColumnFeignService;
import com.gimi.cloud.jimi.manage.consumer.service.app.MajorAdService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @auther loongs
 * @date 2017/12/29  11:39
 */
@Service
public class MajorAdServiceImpl implements MajorAdService {
    private static final Logger logger = LoggerFactory.getLogger(MajorAdServiceImpl.class);
    private AdAndColumnFeignService adAndColumnFeignService;
    @Autowired
    public MajorAdServiceImpl(AdAndColumnFeignService adAndColumnFeignService) {
        this.adAndColumnFeignService = adAndColumnFeignService;
    }

    @Override
    public Response getMajorAdList(String requestBody, HttpServletRequest request, HttpServletResponse response) throws MsgException {
        logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
        Map<String,String> map=JsonUtil.jsonToMap(requestBody);
        String pageId = map.get("pageId");
        if(!StringUtil.isBlank(pageId) && pageId.length() > 9){
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR,"页面编号填写数字，且不能超过9位");
        }

        MajorAdDTO majorAdDTO = JsonUtil.jsonToBean(requestBody,MajorAdDTO.class);
        if(StringUtil.isBlank(majorAdDTO.getPageSize())|| StringUtil.isBlank(majorAdDTO.getCurrentPage())){
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        return adAndColumnFeignService.getMajorAdList(majorAdDTO);
    }

    @Override
    public Response majorAdRelyColumnProd(String requestBody, HttpServletRequest request, HttpServletResponse response) throws MsgException {
        logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
        MajorAdDTO majorAdDTO = JsonUtil.jsonToBean(requestBody,MajorAdDTO.class);
        if( StringUtil.isBlank(majorAdDTO.getCurrentPage()) || StringUtil.isBlank(majorAdDTO.getPageSize())){
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        return adAndColumnFeignService.majorAdRelyColumnProd(majorAdDTO);
    }

    @Override
    public Response majorAdRelyColumn(String requestBody, HttpServletRequest request, HttpServletResponse response) throws MsgException {
        logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
        MajorAdDTO majorAdDTO = JsonUtil.jsonToBean(requestBody,MajorAdDTO.class);
        return adAndColumnFeignService.majorAdRelyColumn(majorAdDTO);
    }

    @Override
    public Response AddMajorAd(String requestBody, HttpServletRequest request, HttpServletResponse response) throws MsgException {
        logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
        //判断数字长度
        Validates.checkNumLength(requestBody);
        MajorAdDTO majorAdDTO = JsonUtil.jsonToBean(requestBody,MajorAdDTO.class);
        if(StringUtil.isBlank(majorAdDTO.getType())
                ||StringUtil.isBlank(majorAdDTO.getComent())||StringUtil.isBlank(majorAdDTO.getPageId())||StringUtil.isBlank(majorAdDTO.getImgUrl())
                ||StringUtil.isBlank(majorAdDTO.getPositionNum())||StringUtil.isBlank(majorAdDTO.getTitle())){
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        //判断广告类型
        checkAdClassAndAdObject(majorAdDTO);
        return adAndColumnFeignService.addMajorAd(majorAdDTO);
    }



    /**
     * 判断广告类型
     * @param majorAdDTO
     */
    private void checkAdClassAndAdObject(MajorAdDTO majorAdDTO){
        //如果广告类型为3，说明广告类别是静态页面的转跳路径
        if(3 == majorAdDTO.getAdClass()){
            if(StringUtil.isBlank(majorAdDTO.getTargetUrl())){
                throw new MsgException(ResponseCodeEnum.TARGET_URL_IS_EMPTY);
            }
        }
        //如果广告类型为1，商品名称不能为空
        if(1 == majorAdDTO.getAdClass()){
            if(StringUtil.isBlank(majorAdDTO.getAdObject())){
                throw new MsgException(ResponseCodeEnum.MAJORAD_PRODUCT_IS_NOT_EMPTY);
            }
        }
        //如果广告类型为2，栏目名称不能为空
        if(2 == majorAdDTO.getAdClass()){
            if(StringUtil.isBlank(majorAdDTO.getAdObject())){
                throw new MsgException(ResponseCodeEnum.MAJORAD_COLUMN_IS_NOT_EMPTY);
            }
        }
    }

    @Override
    public Response editMajorAd(String requestBody, HttpServletRequest request, HttpServletResponse response) throws MsgException {
        logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
        //判断数字长度
        Validates.checkNumLength(requestBody);
        MajorAdDTO majorAdDTO = JsonUtil.jsonToBean(requestBody,MajorAdDTO.class);
        if(StringUtil.isBlank(majorAdDTO.getType())||StringUtil.isBlank(majorAdDTO.getId())
                ||StringUtil.isBlank(majorAdDTO.getComent())||StringUtil.isBlank(majorAdDTO.getPageId())||StringUtil.isBlank(majorAdDTO.getImgUrl())
                ||StringUtil.isBlank(majorAdDTO.getPositionNum())||StringUtil.isBlank(majorAdDTO.getTitle())){
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        //判断广告类型
        checkAdClassAndAdObject(majorAdDTO);
        return adAndColumnFeignService.editMajorAd(majorAdDTO);
    }

    @Override
    public Response gotoEditMajorAd(String requestBody, HttpServletRequest request, HttpServletResponse response) throws MsgException {
        logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
        MajorAdDTO majorAdDTO = JsonUtil.jsonToBean(requestBody,MajorAdDTO.class);
        if(StringUtil.isBlank(majorAdDTO.getId())){
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        return adAndColumnFeignService.gotoEditMajorAd(majorAdDTO);
    }

    @Override
    public Response deleteMajorAd(String requestBody, HttpServletRequest request, HttpServletResponse response) throws MsgException {
        logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
        MajorAdDTO majorAdDTO = JsonUtil.jsonToBean(requestBody,MajorAdDTO.class);
        if(StringUtil.isBlank(majorAdDTO.getId())){
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        return adAndColumnFeignService.deleteMajorAd(majorAdDTO);
    }

    @Override
    public Response publishMajorAd(String requestBody, HttpServletRequest request, HttpServletResponse response) throws MsgException {
        logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
        MajorAdDTO majorAdDTO = JsonUtil.jsonToBean(requestBody,MajorAdDTO.class);
        if(StringUtil.isBlank(majorAdDTO.getId())||StringUtil.isBlank(majorAdDTO.getPublishTime())||StringUtil.isBlank(majorAdDTO.getEndTime())){
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        return adAndColumnFeignService.publishMajorAd(majorAdDTO);
    }

    @Override
    public Response underMajorAd(String requestBody, HttpServletRequest request, HttpServletResponse response) throws MsgException {
        logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
        MajorAdDTO majorAdDTO = JsonUtil.jsonToBean(requestBody,MajorAdDTO.class);
        if(StringUtil.isBlank(majorAdDTO.getId())){
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        return adAndColumnFeignService.underMajorAd(majorAdDTO);
    }

    @Override
    public Response uploadMajorAdFile(HttpServletResponse response, HttpServletRequest request) throws Exception {
        StandardMultipartHttpServletRequest multipartHttpServletRequest = (StandardMultipartHttpServletRequest) request;
        MultiValueMap multipartFiles = multipartHttpServletRequest.getMultiFileMap();
        return adAndColumnFeignService.uploadMajorAdFile(multipartFiles);
    }
}
